package bigbeard.tools.net.client;


import bigbeard.tools.net.api.DataArrivalCallback;
import bigbeard.tools.net.api.StateChangeCallback;
import bigbeard.tools.net.api.client.IClient;
import bigbeard.tools.thread.pools.ThreadPoolFactory;

import java.util.concurrent.ExecutorService;

/**
 * @author bigbeard on 2022-09-29
 */
public abstract class BaseClientImpl implements IClient {

    private static final String THREAD_NAME_PREFIX = "net-notify-thread-%s";
    protected DataArrivalCallback dataArrivalCallback;
    protected StateChangeCallback stateChangeCallback;
    private final ExecutorService executorService = ThreadPoolFactory.createNotifyThreadPool(THREAD_NAME_PREFIX);

    @Override
    public void onDataArrival(DataArrivalCallback callback) {
        dataArrivalCallback = callback;
    }

    @Override
    public void onStateChange(StateChangeCallback callback) {
        this.stateChangeCallback = callback;
    }

    protected void notifyState(int state, String info) {
        if (null != stateChangeCallback) {
            executorService.execute(() -> stateChangeCallback.notifyStateChange(getClientKey(), state, info));
        }
    }

    protected void notifyDataArrival(byte[] data) {
        if (null != dataArrivalCallback) {
            dataArrivalCallback.callback(getClientKey(), data);
        }
    }

    protected String getClientKey() {
        return "";
    }
}
